<?php
// +----------------------------------------------------------------------
// | Leaps Framework [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011-2014 Leaps Team (http://www.tintsoft.com)
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author XuTongle <xutongle@gmail.com>
// +----------------------------------------------------------------------
use Leaps\HttpClient;
class Baidu
{
	/**
	 * http client 客户端类
	 *
	 * @var Leaps\HttpClient
	 */
	private $http;

	/**
	 * 构造方法
	 */
	public function __construct()
	{
		$this->http = new HttpClient ();
	}

	/**
	 * 获取收录数量
	 */
	public function getSite($domain)
	{
		$url = "http://www.baidu.com/s?wd=site:{$domain}";
		return $this->handle ( $url );
	}

	/**
	 * 获取百度快照日期
	 */
	public function getSnapshot($domain)
	{
		$url = "http://www.baidu.com/s?wd=site:{$domain}";
		$this->http->setUserAgent('Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10');
		$response = $this->http->get ( $url );
		if ($response->getHttpCode () == '200') {
			$response = $this->substrBetween($response->getBody(),"<cite><b>{$domain}</b>/</cite><time>",'</time>');
			return $response;
		} else {
			return '';
		}
	}

	/**
	 * 获取反联数量
	 *
	 * @param string $domain
	 */
	public function getLink($domain)
	{
		$url = "http://www.baidu.com/s?wd=domain:{$domain}";
		return $this->handle ( $url );
	}

	/**
	 * 获取24小时收录数量
	 *
	 * @param string $domain
	 */
	public function getSite24Hours($domain)
	{
		$url = "http://www.baidu.com/s?wd=site:{$domain}&tn=baiduadv&lm=1";
		return $this->handle ( $url );
	}

	/**
	 * 获取7天收录数量
	 *
	 * @param string $domain
	 */
	public function getSite7Day($domain)
	{
		$url = "http://www.baidu.com/s?wd=site:{$domain}&tn=baiduadv&lm=7";
		return $this->handle ( $url );
	}

	/**
	 * 获取30天收录数量
	 *
	 * @param string $domain
	 */
	public function getSite30Day($domain)
	{
		$url = "http://www.baidu.com/s?wd=site:{$domain}&tn=baiduadv&lm=30";
		return $this->handle ( $url );
	}

	/**
	 * 处理请求
	 */
	private function handle($url)
	{
		$response = $this->http->get ( $url );
		if ($response->getHttpCode () == '200') {
			$mode = '/找到相关结果约(.*)个/U';
			preg_match ( $mode, $response->getBody (), $result );
			return $result [1];
		} else {
			return 0;
		}
	}

	/**
	 * 提取两个字符串之间的值，不包括分隔符
	 *
	 * @param string $string 待提取的只付出
	 * @param string $start 开始字符串
	 * @param string|null $end 结束字符串，省略将返回所有的。
	 * @return bool string substring between $start and $end or false if either
	 *         string is not found
	 *
	 */
	private function substrBetween($string, $start, $end = null) {
		if (($start_pos = strpos ( $string, $start )) !== false) {
			if ($end) {
				if (($end_pos = strpos ( $string, $end, $start_pos + strlen ( $start ) )) !== false) {
					return substr ( $string, $start_pos + strlen ( $start ), $end_pos - ($start_pos + strlen ( $start )) );
				}
			} else {
				return substr ( $string, $start_pos );
			}
		}
		return false;
	}
}